package com.lyncs.ods.modules.company.service.impl;

import com.lyncs.ods.constant.LyncsOdsConstant;
import com.lyncs.ods.modules.company.model.CompanyRelationRole;
import com.lyncs.ods.modules.company.mapper.CompanyRelationRoleMapper;
import com.lyncs.ods.modules.company.model.CompanyRelationRoleCount;
import com.lyncs.ods.modules.company.service.CompanyRelationRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyncs.ods.resp.CompanyGroupInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业关系权限信息 服务实现类
 * </p>
 *
 * @author alex
 * @since 2022-07-11
 */
@Service
public class CompanyRelationRoleServiceImpl extends ServiceImpl<CompanyRelationRoleMapper, CompanyRelationRole> implements CompanyRelationRoleService {

    @Override
    public List<CompanyRelationRoleCount> getValidRoleCount(@NotNull Long companyId, List<Long> relationIds) {
        if (CollectionUtils.isEmpty(relationIds)) {
            return List.of();
        }
        return this.baseMapper.getValidRoleCount(companyId, relationIds);
    }

    @Override
    public List<Long> getRelationIdsByGroupIds(@NotNull Long companyId, List<Long> groupIds) {
        if (CollectionUtils.isEmpty(groupIds)) {
            return List.of();
        }
        List<CompanyRelationRole> list = this.lambdaQuery().eq(CompanyRelationRole::getCompanyId, companyId)
                .in(CompanyRelationRole::getGroupId, groupIds)
                .eq(CompanyRelationRole::getStatus, LyncsOdsConstant.EnableStatus.ENABLE.getKey())
                .list();
        return list.stream().map(CompanyRelationRole::getRelationId).collect(Collectors.toList());
    }

    @Override
    public Map<Long, List<CompanyGroupInfo>> getRelationGroup(@NotNull Long companyId, List<Long> relationIds) {
        if (CollectionUtils.isEmpty(relationIds)) {
            return Map.of();
        }
        List<CompanyGroupInfo> groupInfos = this.baseMapper.getRelationGroupInfo(companyId, relationIds);
        return CollectionUtils.isEmpty(groupInfos) ? Map.of() : groupInfos.stream().collect(Collectors.groupingBy(CompanyGroupInfo::getRelationId));
    }

    @Override
    public void saveGroups(Long companyId, Long relationId, List<Long> groupIds) {
        //remove
        this.lambdaUpdate().eq(CompanyRelationRole::getCompanyId, companyId).eq(CompanyRelationRole::getRelationId, relationId).remove();
        //insert
        if (CollectionUtils.isEmpty(groupIds)) {
            return;
        }
        List<CompanyRelationRole> newGroups = groupIds.stream().map(id -> new CompanyRelationRole(relationId, companyId, id)).collect(Collectors.toList());
        this.saveBatch(newGroups);
    }

    @Override
    public List<CompanyGroupInfo> getGroupCompanyInfos(@NotNull Long companyId, List<Long> groupIds) {
        if (CollectionUtils.isEmpty(groupIds)) {
            return List.of();
        }
        return this.baseMapper.getGroupCompanyInfos(companyId, groupIds);
    }

}
